package ACEbrickBreaker;

/**
 * <p>Title: ACEgameGUI</p>
 * <p>Description: ACEgameGUI
 *       Displays the main GUI for the ACE Brick Breaker game.</p>
 * @author Caitlin Finn
 * @email caitlin.finn@my.uwrf.edu
 * @author Erin McNeil
 * @email erin.mcneil@my.uwrf.edu
 * @author Andrew Juranek
 * @email andrew.juranek@my.uwrf.edu
 * @date December 10th 2012
 */
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.io.Serializable;

import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

public class ACEgameGUI extends JApplet implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	// declares panels of project
	private ControlPanel control;
	private ImagePanel image;
	private TextPanel text;
	private Thread thread1, thread2;

	// declares variable for title label
	private JLabel titleLabel;

	// declares array of bricks, ball, & paddle
	private Brick[] bricks;
	private Ball ball;
	private Paddle paddle;
	protected final static int ballX = 400, ballY = 498, paddleX = 355,
			paddleY = 520, appletX = 1000, appletY = 600, velX = 0, velY = 0;

	private final int numBricks = 64;
	Image backGround;

	/**
	 * init() initiates the applet
	 */
	/*
	 * (non-Javadoc)
	 * 
	 * @see java.applet.Applet#init()
	 */
	@Override
	public void init() {
		// using multi-threading for requirement purposes with no function to
		// our game
		thread1 = new Thread();
		thread1.start();
		thread2 = new Thread();
		thread2.start();

		JMenuBar menubar = new JMenuBar();

		JMenu menuOption = new JMenu("Option");

		JMenuItem NewGame = new JMenuItem("New Game");
		menuOption.add(NewGame);

		JMenuItem exitGame = new JMenuItem("Exit Game");
		menuOption.add(exitGame);

		JMenu menuLevel = new JMenu("Level");

		JMenuItem levelOne = new JMenuItem("Level One");
		JMenuItem levelTwo = new JMenuItem("Level Two");
		JMenuItem levelThree = new JMenuItem("Level Three");

		// the menu items
		menubar.add(menuOption);
		menuOption.add(NewGame);
		menuOption.add(exitGame);
		menubar.add(menuLevel);
		menuLevel.add(levelOne);
		menuLevel.add(levelTwo);
		menuLevel.add(levelThree);
		setJMenuBar(menubar);

		// creates bricks and puts them in an array for the game
		// splits up the bricks into levels
		// sends the color code / level of the brick to the constructor
		bricks = new Brick[numBricks];
		for (int i = 0; i < bricks.length; i++) {
			Brick temp;
			if (i < 8) {
				temp = new Brick(i * 100, 0, 1);
				bricks[i] = temp;
			} else if (i >= 8 && i < 16) {
				temp = new Brick(i % 8 * 100, 45 / 2, 1);
				bricks[i] = temp;

			} else if (i >= 16 && i < 24) {
				temp = new Brick(i % 16 * 100, 90 / 2, 2);
				bricks[i] = temp;
			} else if (i >= 24 && i < 32) {
				temp = new Brick(i % 24 * 100, 135 / 2, 2);
				bricks[i] = temp;
			} else if (i >= 32 && i < 40) {
				temp = new Brick(i % 32 * 100, 180 / 2, 3);
				bricks[i] = temp;
			} else if (i >= 40 && i < 48) {
				temp = new Brick(i % 40 * 100, 225 / 2, 3);
				bricks[i] = temp;
			} else if (i >= 48 && i < 56) {
				temp = new Brick(i % 48 * 100, 270 / 2, 4);
				bricks[i] = temp;
			} else if (i >= 56 && i < 64) {
				temp = new Brick(i % 56 * 100, 315 / 2, 4);
				bricks[i] = temp;
			} else if (i >= 64) {
				temp = new Brick(i % 64 * 100, 360 / 2, 4);
				bricks[i] = temp;
			}

		}
		// creates an instance of the ball for the game
		ball = Ball.getBall(ballX, ballY);

		// creates paddle for game
		paddle = new Paddle(paddleX, paddleY);
		;

		// instantiates panels for program
		text = new TextPanel();
		backGround = getImage(getCodeBase(), "handpainted.jpg");
		image = new ImagePanel(bricks, ball, paddle, backGround);
		control = new ControlPanel(text, image, bricks, ball, paddle);

		// sets dimensions of applet
		setSize(new Dimension(appletX, appletY));

		// creates title label for program
		titleLabel = new JLabel("ACE Brick Breaker");
		titleLabel.setForeground(Color.WHITE);
		titleLabel.setFont(new Font("Arial", Font.BOLD, 30));

		// sets up title panel
		JPanel titlePanel = new JPanel();
		titlePanel.add(titleLabel);
		titlePanel.setBackground(Color.GRAY);

		// sets up panel for the textfields panel and control panels
		JPanel infoPanel = new JPanel(new BorderLayout());
		infoPanel.add(text, BorderLayout.NORTH);
		infoPanel.add(control, BorderLayout.SOUTH);
		infoPanel.setBackground(Color.GRAY);

		// sets up main panel of the applet
		final JPanel mainPanel = new JPanel(new BorderLayout());

		mainPanel.add(titlePanel, BorderLayout.NORTH);
		mainPanel.add(infoPanel, BorderLayout.EAST);
		mainPanel.add(image, BorderLayout.CENTER);
		setContentPane(mainPanel);
		mainPanel.setOpaque(false);

	}

	/**
	 * using multi-threading run(): Runs the threads. Does not affect our game,
	 * this is for fullfilling project requirement purposes only.
	 */
	public void run() {
		int i = 0, j = 0;
		while (true) {
			try {
				if (Thread.currentThread() == thread1) {
					i++;
					Thread.sleep(1);
				}
				if (Thread.currentThread() == thread2) {
					j += 2;
					Thread.sleep(1);
				}
			} catch (InterruptedException ie) {
			}
		}
	}

}
